Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

190
Visualizações
Why my "change" event hasn't worked as intended?

Okay, so I'm practicing my JS and HTML, my current code looks like this:

document.querySelector('input[name="answer"]:checked').checked = false;
let Select = document.querySelector('input[name="answer"]');
Select.addEventListener('change', function(event) {
  alert(event.target.value)
})
<div>
  <label><input type="radio" name="answer" value="1" checked="checked">1</label>
  <label><input type="radio" name="answer" value="2">2</label>
</div>

My aim is to cancel first radio's "checked", then add an event that will show each of radio's value whenever I choose/"checked" it, but as you may notice, so far the alert will only shows up if I choose the first radio. So if I may ask, where did I do wrong and how can I fix it?

p.s. For the sake of practicing, the HTML has to stay the same. No adding Id or other selector.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

  1. You only get the first with querySelector
  2. The querySelector(...:checked) will work because you can only have one checked radio
  3. I really like to delegate - it is recommended and makes a lot of sense

document.querySelector('input[name="answer"]:checked').checked = false;
let radDiv = document.getElementById('radioDiv');
radDiv.addEventListener('change', function(event) {
  const tgt = event.target;
  if (tgt.matches("[type=radio][name=answer]")) { // check we have the right elements
    alert(tgt.value)
  }
})
<div id="radioDiv">
  <label><input type="radio" name="answer" value="1" checked="checked">1</label>
  <label><input type="radio" name="answer" value="2">2</label>
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

document.querySelector('input[name="answer"]:checked').checked = false;

let Select = document.querySelectorAll('input[name="answer"]');

 Select.forEach(el => el.addEventListener('change',function (event) {
    Select.forEach(e => alert(e.value));
 }))
<div>
      <label><input type="radio" name="answer" value="1" checked="checked">1</label>
      <label><input type="radio" name="answer" value="2">2</label>
    </div>

about 4 years ago · Juan Pablo Isaza Relatório

0

let Select = document.querySelectorAll('input[name="answer"]');
Select.forEach(function(elem) {
    elem.addEventListener("change", function(event) {
        alert(event.target.value)
    });
});

Use above for element selector instead of querySelector which returns a single object (first match) querySelectorAll returns an array of elements.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda